package sliver.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sliver.dao.TagDao;
import sliver.dao.TagLogDao;
import sliver.entity.Tag;
import sliver.service.TagManager;

@Service
@Transactional
public class TagManagerImpl implements TagManager{

	@Autowired
	private TagDao tagDao;
	
	@Autowired
	private TagLogDao tagLogDao;
	
	@Override
	public List<Tag> obtainAllTags() {
		
		List<Tag> tags = tagDao.findAll(Tag.class);
		return tags;
	}

	@Override
	public Double calculateCpm(List<Integer> tagIds, Integer position, List<Long> provinceIds, String showTimes) {
		
		Double suggestPrice = 0D;
		
		List<Object> prices = tagLogDao.obtainEachTagAvgPrice(tagIds,position,provinceIds,showTimes);
		for(Object price : prices){
			Double p = Double.valueOf((String)price);
			suggestPrice += p;
		}
		
		if(suggestPrice == 0D)
			return 1D;
		
		return suggestPrice;
	}
}
